<?php
App::uses('AppController', 'Controller');
/**
 * Cursos Controller
 *
 * @property Curso $Curso
 */
class CursosController extends AppController {

	public function beforeFilter(){
		parent::beforeFilter();
	}
/**
 * index method
 *
 * @return void
 */
	public function index() {
		if(!$this->isAdmin()){
			$this->notAuth();
		}
		$this->Curso->recursive = 0;
		$this->set('cursos', $this->paginate());
	}

	public function add() {
		if(!$this->isAdmin()){
			$this->notAuth();
		}
		if ($this->request->is('post')) {
			$this->Curso->create();
			if ($this->Curso->salvar($this->request->data)) {
				$this->Session->setFlash(__('Curso salvo!'), 'flash/bootstrap', array('class'=>'alert alert-success'));
				$this->redirect('/datas_cursos/add/'.$this->Curso->id);
			} else {
				$this->Session->setFlash(__('Curso não pôde ser salvo!'), 'flash/bootstrap', array('class'=>'alert alert-error'));
			}
		}
	}

	public function edit($id = null) {
		if(!$this->isAdmin()){
			$this->notAuth();
		}
		if (!$this->Curso->exists($id)) {
			throw new NotFoundException(__('Curso inválido'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Curso->salvar($this->request->data)) {
				$this->Session->setFlash(__('Curso salvo!'), 'flash/bootstrap', array('class'=>'alert alert-success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Curso não pôde ser salvo!'), 'flash/bootstrap', array('class'=>'alert alert-error'));
			}
		} else {
			$options = array('conditions' => array('Curso.' . $this->Curso->primaryKey => $id));
			$this->request->data = $this->Curso->find('first', $options);
			$this->request->data['Curso']['preco'] = str_replace('.', ',', $this->request->data['Curso']['preco']);
			$this->request->data['Curso']['preco_material'] = str_replace('.', ',', $this->request->data['Curso']['preco_material']);
		}
	}

	public function delete($id = null) {
		if(!$this->isAdmin()){
			$this->notAuth();
		}
		$this->Curso->id = $id;
		if (!$this->Curso->exists()) {
			throw new NotFoundException(__('Curso inválido'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Curso->delete()) {
			$this->Session->setFlash(__('Curso excluído'), 'flash/bootstrap', array('class'=>'alert alert-success'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Curso não pôde ser excluído!'), 'flash/bootstrap', array('class'=>'alert alert-error'));
		$this->redirect(array('action' => 'index'));
	}
	
	
	public function precos_cursos(){
		$this->layout = false;
		$this->autoRender = false;
		$preco = 0;
		if(!isset($this->request->data['precos']))
			return 0;
		foreach($this->request->data['precos'] as $item){
			$preco += $this->Curso->preco($item);
		}
		return $preco;
	}
	
	public function precos_materiais(){
		$this->layout = false;
		$this->autoRender = false;
		$preco = 0;
		if(!isset($this->request->data['precos']))
			return 0;
		foreach($this->request->data['precos'] as $item){
			$preco += $this->Curso->preco_material($item);
		}
		return $preco;
	}
}
